#include "CCLuaEngine.h"
#include "tolua++.h"
#include "tolua_fix.h"
#include "LuaCCEditBox.h"

MyCCEditBox* MyCCEditBox::create(CCSize size, const char* bkImgName, const char* placeHolder){
  CCLog("MyCCEditBox::create %s %s", bkImgName, placeHolder);
  MyCCEditBox *editbox = new MyCCEditBox();
  editbox->initWithSizeAndBackgroundSprite(size, CCScale9Sprite::create(bkImgName));
  editbox->setPlaceHolder(placeHolder);
  editbox->setReturnType(kKeyboardReturnTypeDone);
  editbox->setDelegate(editbox);
  editbox->autorelease();
  return editbox;
}

void MyCCEditBox::editBoxEditingDidBegin(cocos2d::extension::CCEditBox* editBox){
  CCLog("editBox %p DidBegin !", editBox);
}

void MyCCEditBox::editBoxEditingDidEnd(cocos2d::extension::CCEditBox* editBox){
  CCLog("editBox %p DidEnd, %s", editBox, editBox->getText());
}

void MyCCEditBox::editBoxTextChanged(cocos2d::extension::CCEditBox* editBox, const std::string& text){
  CCLog("editBox %p TextChanged, text: %s ", editBox, text.c_str());
}

void MyCCEditBox::editBoxReturn(CCEditBox* editBox){
  CCLog("editBox %p was returned, %s", editBox, editBox->getText()); 
  CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
  pEngine->executeHandleFunction("onEditBoxReturn", this, CCControlEventTouchDown);
}

int tolua_Cocos2d_CCEditBox_create(lua_State* tolua_S){
  CCSize size = *((CCSize*)tolua_tousertype(tolua_S,2,0));
  const char* imgName = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* placeHolder = ((const char*)  tolua_tostring(tolua_S,4,0));
  MyCCEditBox *editbox = MyCCEditBox::create(size, imgName, placeHolder);
  MyCCEditBox* tolua_ret = editbox;
  int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
  int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
  toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCEditBox");
  return 1;
}

int tolua_Cocos2d_CCEditBox_getText(lua_State* tolua_S){
  MyCCEditBox* self = (MyCCEditBox*)  tolua_tousertype(tolua_S,1,0);
  const char* tolua_ret = self->getText(); 
  tolua_pushstring(tolua_S,(const char*)tolua_ret);
  return 1;
}
int tolua_Cocos2d_CCEditBox_setText(lua_State* tolua_S){
  MyCCEditBox* self = (MyCCEditBox*)  tolua_tousertype(tolua_S,1,0);
  const char* text = ((const char*)  tolua_tostring(tolua_S,2,0));
  self->setText(text);
  return 1;
}
int tolua_Cocos2d_CCEditBox_setInputFlag(lua_State* tolua_S){
  MyCCEditBox* self = (MyCCEditBox*)  tolua_tousertype(tolua_S,1,0);
  int flag = ((int)  tolua_tonumber(tolua_S,2,0));
  self->setInputFlag((EditBoxInputFlag)flag);
  return 1;
}
